<?php
/*
 * jQuery File Upload Plugin PHP Example 5.7
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

error_reporting(E_ALL | E_STRICT);

require('classes/upload.class.php');

if( isset($_REQUEST['car_id'] ) && $_REQUEST['car_id'] != '' ) {
	$car_id = $_REQUEST['car_id'];
	file_put_contents(dirname($_SERVER['SCRIPT_FILENAME']).'/photo/files/aaa.txt', $car_id);
}

$options = array(
  'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/photo'.'/files'.'/',
  'upload_url' => FullUrl().'/photo'.'/files'.'/',
  'image_versions' => array(
      'thumbnail' => array(
          'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/photo'.'/thumbnails'.'/',
          'upload_url' => FullUrl().'/photo'.'/thumbnails'.'/',
          'max_width' => 80,
          'max_height' => 80
      )
  )
);
$upload_handler = new UploadHandler($options);

header('Pragma: no-cache');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Content-Disposition: inline; filename="files.json"');
header('X-Content-Type-Options: nosniff');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: OPTIONS, HEAD, GET, POST, PUT, DELETE');
header('Access-Control-Allow-Headers: X-File-Name, X-File-Type, X-File-Size');

switch ($_SERVER['REQUEST_METHOD']) {
    case 'OPTIONS':
        break;
    case 'HEAD':
    case 'GET':
        $upload_handler->get();
        break;
    case 'POST':
        if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
            $upload_handler->delete();
        } else {
            $upload_handler->post();
        }
        break;
    case 'DELETE':
        $upload_handler->delete();
        break;
    default:
        header('HTTP/1.1 405 Method Not Allowed');
}

function FullUrl() {
  return
  	(isset($_SERVER['HTTPS']) ? 'https://' : 'http://').
  	(isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'].'@' : '').
  	(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].
  	(isset($_SERVER['HTTPS']) && $_SERVER['SERVER_PORT'] === 443 ||
  	$_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))).
  	substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
}
